<template>
  <div class="tab-bar weui-flex ta-c v-mdl bg-white">
    <div class="weui-flex__item" @tap.stop="onGoHomework">
      <div style="position: relative;width: 30px;height: 30px;margin: 0 auto;">
        <div v-if="currentTab == 'homework'" class="tab-icon hw-selected"></div>
        <div v-else class="tab-icon hw-unselected"></div>
        <span v-if="unreadCount">{{unreadCount}}</span>
      </div>
      <div class="label">作业</div>
    </div>
    <div class="weui-flex__item" @tap.stop="onGoUser">
      <div v-if="currentTab == 'user'" class="tab-icon user-selected"></div>
      <div v-else class="tab-icon user-unselected"></div>
      <div class="label">我</div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import * as $task from '@/api/task'
import * as $user from '@/api/user'
export default {
  props: {
    current: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      totalUnreadCount: 0,
      totalProgressUnreadCount: 0
    }
  },
  computed: {
    ...mapGetters([
      'currentTab',
      'role',
      'isLogined'
    ]),
    ...mapGetters('document', [
      'unreadCount',
    ]),
    unreadCount () {
      if (this.role === 'teacher') return this.totalUnreadCount
      return this.totalProgressUnreadCount
    }
  },
  methods: {
    ...mapActions([
      'setCurrentTab'
    ]),
    onGoHomework () {
      this.$store.commit('progress/PROGRESS_RESET_PROGRESSES', [])
      this.$store.commit('document/DOCUMENT_SET_DOCUMENTS', [])
      this.$store.commit('task/TASK_SET_TASKS', [])
      if (this.role === 'teacher') {
        wx.reLaunch({
          url: '/pages/documents/main'
        })
      } else {
        wx.reLaunch({
          url: '/pages/user-progresses/main'
        })
      }
      this.setCurrentTab('homework')
    },
    onGoUser () {
      wx.reLaunch({
        url: '/pages/user/main'
      })
      this.setCurrentTab('user')
    }
  },
  watch: {
    async isLogined (nv) {
      if (!nv) return
      this.totalUnreadCount = await $task.getTotalUnreadCount()
      this.totalProgressUnreadCount = await $user.getMyTotalProgressUnreadCount()
    }
  }
}
</script>

<style scoped lang="less">
@import '../assets/less/var.less';
.tab-bar {
  position: fixed;
  width: 100%;
  height: 5 * @baseSize;
  bottom: 0;
  left: 0;
  font-size: @fs-xxs;
  padding-top: 2px;
  border-top: 1rpx solid #ddd;
}
img {
  display: inline-block;
  width: 22px;
  height: 22px;
}
.label {
  font-size: 10px;
  color: #333;
}
.tab-icon {
  width: 28px;
  height: 28px;
  background-size: 22px 22px;
  background-position: center;
  background-repeat: no-repeat;
  margin: 0 auto;
  &.hw-selected {
    background-image: url("../assets/icon/tab-homework-selected.png");
  }
  &.hw-unselected {
    background-image: url("../assets/icon/tab-homework-unselected.png");
  }
  &.user-selected {
    background-image: url("../assets/icon/tab-user-selected.png");
  }
  &.user-unselected {
    background-image: url("../assets/icon/tab-user-unselected.png");
  }
}

span {
  position: absolute;
  right: -10px;
  top: 0;
  background-color: @colorRed;
  color: white;
  font-size: 10px;
  display: inline;
  padding: 0 5px;
  border-radius: 8px;
}
</style>
